package 排序数组中缺失的数字;

public class Solution
{
    //蛮力法，遍历数组，依次求后一个数字减当前数字的差值，为2就找到了
    //二分法
    public int missingNumber(int[] nums)
    {
        int res = 0;
        if (nums.length == 1)
        {
            return nums[0];
        }
        if (nums != null && nums.length != 0)
        {
            res = missingNumber(nums, 0, nums.length - 1);
        }
        return res;
    }

    public int missingNumber(int[] nums, int left, int right)
    {
        if (left > right)
            return -1;
        if (nums[left] != left)
            return left;
        if (nums[right] != right)
            return right;
        int mid = (left + right) / 2;
        if (nums[mid] == mid)
            left = mid;
        else if (nums[mid] != mid)
            right = mid;
        return missingNumber(nums, left, right);
    }

    public int missingNumber1(int[] nums)
    {
        //循环二分法
        int i = 0;
        int j = nums.length - 1;
        while (i <= j)
        {
            int m = (i + j) / 2;
            if (nums[m] == m) i = m + 1;
            else j = m - 1;
        }
        return i;
    }

    public static void main(String[] args)
    {
        Solution solution = new Solution();
        System.out.println(solution.missingNumber(new int[]{0, 1}));
    }
}
